/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.transas.zorlog.data;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author amalikov
 */
public class DataSourcePlain extends DataSource {

	@Override
	public Object getHandle(String name) {
		int n = 0;
		for (String s : names) {
			if (s.equals(name))
				return new Integer(n);
			++n;
		}
		return new Integer(-1);
	}

	@Override
	public double getValue(Object handle, int index) {
		final int n = (Integer)handle;
		return (n >= 0 && n < names.length && index < values.size()) ? values.get(index)[n] : 0;
	}

	@Override
	public String getValueAsString(Object handle, int index, NumberFormat nf) {
		nf.setMaximumFractionDigits(6);
		nf.setMinimumFractionDigits(0);
		return nf.format(getValue(handle, index));
	}

	@Override
	public double[] getValues(String name) {
		final int n = (Integer)getHandle(name);
		if (n >= 0 && n < names.length) {
			double[] res = new double[values.size()];
			for (int i = 0; i < res.length; ++i)
				res[i] = values.get(i)[n];
			return res;
		}
		else
			return new double[0];
	}

	@Override
	public int getSize() {
		return values.size();
	}

	@Override
	public List<String> getNames() {
		return new ArrayList<>(Arrays.asList(names));
	}
	
	@Override
	public String getAttribute(Object handle, String attrName) {
		return "";
	}	
	
	public void create(String[] names) {
		this.names = names;
		values = new ArrayList<>();
	}	
	
	public void addRow(double[] row) {
		values.add(row);
	}	
	
	private String[] names;
	private List<double[]> values;
}
